//
//  ProdDetailHeaderView.m
//  JiaoX
//
//  Created by yinqy on 15/1/12.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ProdDetailHeaderView.h"
#import "SGFocusImageFrame.h"
#import "PriceBarView.h"
#import "SGFocusImageItem.h"
#import "PromotionView.h"

#define PROD_TITLE_FONT_SIZE 15

@interface ProdDetailHeaderView()
{
    SGFocusImageFrame *_bannerView;
    UILabel *_nameLabel;
    PriceBarView *_priceBarView;
    
}
@property(nonatomic,strong)PromotionView *promotionView;
@end
@implementation ProdDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUserInteractionEnabled:YES];
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setFont:[UIFont systemFontOfSize:PROD_TITLE_FONT_SIZE]];
        [_nameLabel setTextColor:[UIColor blackColor]];
        [self addSubview:_nameLabel];
    }
    return self;
}

-(void)setProdDetail:(ProductDetail *)prodDetail{
    _prodDetail = prodDetail;
    [self setBannerData:prodDetail.prodImages];
    CGSize prodNameSize = [prodDetail.prodName sizeWithFont:[UIFont systemFontOfSize:PROD_TITLE_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
    [_nameLabel setText:prodDetail.prodName];
    [_nameLabel setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_bannerView.frame)+15, JXScreenW-JXPublicInset*2, prodNameSize.height)];
    
    _priceBarView = [[PriceBarView alloc]initWithNowPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodNewPrice] oldPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodOldPrice] buyCount:[NSString stringWithFormat:@"%d",prodDetail.prodBuyCount]];
    [_priceBarView setX:0];
    [_priceBarView setY:CGRectGetMaxY(_nameLabel.frame)+15];
    [self addSubview:_priceBarView];
    
    _promotionView = [[PromotionView alloc]init];
    [_promotionView setPromotion:prodDetail.prodPromotion];
    _promotionView.x = 0;
    _promotionView.y = CGRectGetMaxY(_priceBarView.frame)+15;
    [self addSubview:_promotionView];
    
    [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(_promotionView.frame))];
}


//设置商品头图
-(void)setBannerData:(NSArray *)bannerArray{
    if(!bannerArray.count)return;
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerArray.count+2];
    
    for (int i = 0; i < bannerArray.count; i++)
    {
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:bannerArray[i] image:bannerArray[i] tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerArray.count>1){
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:bannerArray[bannerArray.count-1] image:bannerArray[bannerArray.count-1] tag:bannerArray.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:bannerArray[0] image:bannerArray[0] tag:bannerArray.count-1];
        [itemArray addObject:endItem];
    }
    
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, JXScreenW, JXScreenW) delegate:nil imageItems:itemArray isAuto:NO];
    [bannerView scrollToIndex:0];
    _bannerView = bannerView;
    [self addSubview:_bannerView];
}

- (void)dealloc
{
    [_promotionView invalidateTimer];
}

@end
